<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/2
 * Time: 15:04
 */

namespace app\admin\controller;
use think\Request;
use \think\Db;
use app\common\model\Area as AreaModel;


class Business extends \app\admin\controller\Base
{
    protected $res = array(
        'index'   => array('css' => '2,3,4', 'js' => 'list,My97DatePicker,echart'),
        'column'   => array('css' => '2,3,4', 'js' => 'echart,My97DatePicker'),
        'style'   => array('css' => '2,3,4', 'js' => 'echart,My97DatePicker'),
    );
    
    /**
     * @return mixed
     * 图形展示
     */
    public function index(){
        $AreaModel = new AreaModel();
        $search = input('param.search/a');
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $wherePro['level'] = 1;
        if($search[1]!=''){
            $whereCity['pid'] = $search[1];
        }else{
            $whereCity['pid'] = 2;
        }
        if($search[2]!=''){
            $whereDistrict['pid'] = $search[2];
        }else{
            $whereDistrict['pid'] = 52;
        }
        $province =$AreaModel->selectArea($wherePro);
        $city =$AreaModel->selectArea($whereCity);
        $district =$AreaModel->selectArea($whereDistrict);
        $this->assign(
            array(
                'action'=>$action,
                'search' => $search,
                'province' => $province,
                'city' => $city,
                'district' => $district,
            )
        );
        return $this->fetch();
    }
    /**
     * 图形数据
     */
    public function echartsColumn(){
        $search = input('param.search/a');
        //时间条件
        if($search[3]!=''&&$search[4]!=''){
            $date_time = $this->prDates($search[3],$search[4],$search[5]);
        }else{
            $date_time = $this->prDates();
        }
        
        $result = array();
        foreach ($date_time as $key => $vo){
            $result['time'][]=$vo;
            if($search[5]==1){
                $whereHe['u.dog_time'] = array('elt',$vo);
            }else if($search[5]==2){
                $whereHe['u.dog_month'] = array('elt',$vo);
            }else if($search[5]==3){
                $whereHe['u.dog_year'] = array('elt',$vo);
            }else{
                $whereHe['u.dog_time'] = array('elt',$vo);
            }
            if($search[5]==1){
                $whereHe1['u.dog_time'] = array('EQ',$vo);
            }else if($search[5]==2){
                $whereHe1['u.dog_month'] = array('EQ',$vo);
            }else if($search[5]==3){
                $whereHe1['u.dog_year'] = array('EQ',$vo);
            }else{
                $whereHe1['u.dog_time'] = array('EQ',$vo);
            }
//            $mutualNum1  = $this->getDogNum($search,$whereHe1, 2);//新增互助狗记录
//            $mutualNum2  = $this->getDogNum($search,$whereHe, 2);//累计互助狗记录
            $adoptNum1  = $this->getDogNum($search,$whereHe1, 3);//新增领养记录
            $adoptNum2  = $this->getDogNum($search,$whereHe, 3);//累计领养记录
//            $result['value'][]=$mutualNum1?$mutualNum1:0;
//            $result['value1'][]=$mutualNum2?$mutualNum2:0;
            $result['value2'][]=$adoptNum1?$adoptNum1:0;
            $result['value3'][]=$adoptNum2?$adoptNum2:0;
            
            if($search[5]==1){
                $whereHe2['u.udf_time'] = array('elt',$vo);
            }else if($search[5]==2){
                $whereHe2['u.udf_month'] = array('elt',$vo);
            }else if($search[5]==3){
                $whereHe2['u.udf_year'] = array('elt',$vo);
            }else{
                $whereHe2['u.udf_time'] = array('elt',$vo);
            }
            if($search[5]==1){
                $whereHe3['u.udf_time'] = array('EQ',$vo);
            }else if($search[5]==2){
                $whereHe3['u.udf_month'] = array('EQ',$vo);
            }else if($search[5]==3){
                $whereHe3['u.udf_year'] = array('EQ',$vo);
            }else{
                $whereHe3['u.udf_time'] = array('EQ',$vo);
            }
            $familyNum1  = $this->getFamNum($search,$whereHe3);//新增狗窝记录
            $familyNum2  = $this->getFamNum($search,$whereHe2);//累计狗窝记录
            $result['value4'][]=$familyNum1?$familyNum1:0;
            $result['value5'][]=$familyNum2?$familyNum2:0;
        }
        
        $strayNum  = $this->getNum($search, 1);//流浪狗记录
//        $mutualNum  = $this->getNum($search, 2);//互助狗记录
        $adoptNum  = $this->getNum($search, 3);//求领养记录
        $searchNum  = $this->getNum($search, 4);//寻狗记录
        $familyNum  = $this->getFamilyNum($search);//登记狗记录
        $proNum  = $this->getProposerNum($search);//登记狗记录
        $data = array(
            'result'      =>    $result,
            'strayNum'    =>    $strayNum,
//            'mutualNum'   =>    $mutualNum,
            'adoptNum'    =>    $adoptNum,
            'searchNum'   =>    $searchNum,
            'familyNum'   =>    $familyNum,
            'proNum'      =>    $proNum,
        );
        return json($data);
    }
    /**
     * 获取流浪狗、互助发布、求领养、发布总计 图形数据
     */
    public function getDogNum($search, $whereMap, $type){
        $where = array();
        //地区查询
        if($search[0]!=''){
            $where['u.area_sheng'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.area_shi'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.area_qu'] = array('=',$search[2]);
        }
        //流浪狗
        $where['u.dog_type'] = $type;
        //狗的类别
        $where['u.dog_type'] = $type;
        $dogNum = Db::table('up_dog')
            ->alias('u')
            ->where($where)
            ->where($whereMap)
            ->count();
        return $dogNum;
    }
    /**
     * 获取狗窝总计 图形数据
     */
    public function getFamNum($search, $whereMap){
        $where = array();
        //地区查询
        if($search[0]!=''){
            $where['u.udf_province'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.udf_city'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.udf_district'] = array('=',$search[2]);
        }
        $familyNum = Db::table('up_dog_family')
            ->alias('u')
            ->where($where)
            ->where($whereMap)
            ->count();
        return $familyNum;
    }
    
    /**
     * @param $search    查询条件
     * @param $status   救助状态
     * @return $num     狗 统计记录数量
     */
    public function getNum($search, $type){
        $where = array();
        //地区查询
        if($search[0]!=''){
            $where['u.area_sheng'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.area_shi'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.area_qu'] = array('=',$search[2]);
        }
        //流浪狗
        $where['u.dog_type'] = $type;
        if($type==1){
            $where['dog_num'] = array('>',1);
        }
        //时间条件
        if($search[3] != '' && $search[4] != ''){
            $date_time = $this->prDates($search[3], $search[4], $search[5]);
        }else{
            $date_time = $this->prDates();
        }
        $count = count($date_time);
        if($search[5] == ''){//默认年月日
            $start = $date_time[0];
            $end = $date_time[$count-1];
            $whereMap['u.dog_time'] = array('between',array($start,$end));
        }else{//证明按了点击按钮
            $whereMap = $this->dateArray($search,$count,$date_time);
        }
        //狗的数量
//        $res = Db::table('up_dog')->where($where)->field('sum(dog_num) as num')->select();
//        foreach($res as $key => $val){
//            $num = $val['num'];
//        }
        //记录数
        if($type==1){
            $num = Db::table('up_dog')->alias('u')->where($where)->where($whereMap)->sum('dog_num');
            
        }else{
            $num = Db::table('up_dog')->alias('u')->where($where)->where($whereMap)->count();
            
        }
        return $num;
    }
    
    /**
     * @param $search    查询条件
     * @param $status   救助状态
     * @return $num     狗窝 统计记录数量
     */
    public function getFamilyNum($search){
        $where = array();
        //地区查询
        if($search[0]!=''){
            $where['u.udf_province'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.udf_city'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.udf_district'] = array('=',$search[2]);
        }
        //时间条件
        if($search[3] != '' && $search[4] != ''){
            $date_time = $this->prDates($search[3], $search[4], $search[5]);
        }else{
            $date_time = $this->prDates();
        }
        $count = count($date_time);
        if($search[5] == ''){//默认年月日
            $start = $date_time[0];
            $end = $date_time[$count-1];
            $whereMap['u.udf_time'] = array('between',array($start,$end));
        }else{//证明按了点击按钮
            $whereMap = $this->dateDogArray($search,$count,$date_time);
        }
        //狗的数量
//        $res = Db::table('up_dog')->where($where)->field('sum(dog_num) as num')->select();
//        foreach($res as $key => $val){
//            $num = $val['num'];
//        }
        //记录数
        $num = Db::table('up_dog_family')->alias('u')->where($where)->where($whereMap)->count();
        return $num;
    }
    
    /**
     * @param $search    查询条件
     * @param $status   救助状态
     * @return $num     领养申请总记录
     */
    public function getProposerNum($search){
        $where = array();
        //地区查询
        if($search[0]!=''){
            $where['u.proposer_abode_province'] = array('=',$search[0]);
        }
        if($search[1]!=''){
            $where['u.proposer_abode_city'] = array('=',$search[1]);
        }
        if($search[2]!=''){
            $where['u.proposer_abode_district'] = array('=',$search[2]);
        }
        //时间条件
        if($search[3] != '' && $search[4] != ''){
            $date_time = $this->prDates($search[3], $search[4], $search[5]);
        }else{
            $date_time = $this->prDates();
        }
        $count = count($date_time);
        if($search[5] == ''){//默认年月日
            $start = $date_time[0];
            $end = $date_time[$count-1];
            $whereMap['u.proposer_date'] = array('between',array($start,$end));
        }else{//证明按了点击按钮
            $whereMap = $this->datePorArray($search,$count,$date_time);
        }
        //狗的数量
//        $res = Db::table('up_dog')->where($where)->field('sum(dog_num) as num')->select();
//        foreach($res as $key => $val){
//            $num = $val['num'];
//        }
        //记录数
        $num = Db::table('up_dog_proposer')->alias('u')->where($where)->where($whereMap)->count();
        return $num;
    }
    /**
     * 查询市
     */
    public function city($city){
//        $city = $_POST['city'];
        $cityArray = Db::table('up_area')->where('pid',$city)->select();
        $data['cityArray'] = $cityArray;
        return json($data);
    }
    /**
     * 查询区
     */
    public function area($area){
//        $area = $_POST['area'];
        $cityArray = Db::table('up_area')->where('pid',$area)->select();
        $data['cityArray'] = $cityArray;
        return json($data);
    }
}